package com.sls.web.manager.action.game;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;

import com.sls.web.manager.action.SuperAction;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.common.DataUtil;
import com.sls.web.manager.common.FilterStringSpaces;
import com.sls.web.manager.common.WebTools;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.game.pojo.Goods;
import com.sls.web.manager.game.pojo.GoodsBrand;
import com.sls.web.manager.game.pojo.Supply;
import com.sls.web.manager.game.querybean.GoodsQuery;
import com.sls.web.manager.game.service.GameSpecialPriceService;
import com.sls.web.manager.game.service.GoodsBrandService;
import com.sls.web.manager.game.service.GoodsService;
import com.sls.web.manager.game.service.LocalGoodsNotice;
import com.sls.web.manager.game.service.SupplyManagerService;
import com.sls.web.manager.page.Pagination;

public class GoodsAction extends SuperAction {
	private static final long serialVersionUID = 8582079469945322609L;
	@Resource(name = "goodsService")
	private GoodsService goodsService;
	@Resource(name = "supplyManagerService")
	private SupplyManagerService supplyManagerService;
	@Resource(name = "goodsBrandSerice")
	private GoodsBrandService brandService;
	@Resource(name = "gamePriceService")
	private GameSpecialPriceService gamePriceService;
	private Goods goods;
	private String id;
	private String goodsId;
	private String goodsState;
	private String goId;
	private String brandId;
	private String goodsName;
	private String rechargeMode;
	private GoodsQuery goodsQuery;
	private Pagination<Goods> page;
	private List<Supply> supplyList;
	private List<GoodsBrand> brandList;
	private List<String> templateNameList;
	@Resource(name="localGoodsNotice")
	private LocalGoodsNotice localGoodsNotice;

	/**
	 * 商品变更，主动通知下游
	 * 
	 * @return
	 * @throws Exception
	 */
	public String noticeUser() throws Exception {
		localGoodsNotice.noticeGoodsModify(goodsId,null,getCurrentOperator());
		if (goodsQuery == null) {
			goodsQuery = new GoodsQuery();
			goodsQuery.setPageSize(Constants.INIT_PAGESIZE);
			goodsQuery.setCurrentPage(Constants.INIT_CURRENTPAGE);
		}
		try {
			goodsQuery.setGoodsState("1");
			page = goodsService.queryGoods(goodsQuery, getCurrentOperator());
		} catch (ManagerException e) {
			addActionError(e.getMessage());
		}
		return SUCCESS;
	}

	/**
	 * 添加
	 * 
	 * @return
	 * @throws Exception
	 */
	public String add() throws Exception {
		brandList = brandService.queryAllBrand("1", "1", getCurrentOperator());
		return INPUT;
	}

	/**
	 * 保存
	 * 
	 * @return
	 * @throws Exception
	 */
	public String save() throws Exception {
		try {
			String commonSalePrice = WebTools.getParameter("commonSalePrice");
			String goodsMoney = WebTools.getParameter("goodsMoney");
			goods.setCommonSalePrice(DataUtil.toInt(commonSalePrice));
			goods.setGoodsMoney(DataUtil.toInt(goodsMoney));

			goodsService.add(goods, getCurrentOperator());
			redirectionUrl = "goods!list.htm";
		} catch (ManagerException e) {
			flag = false;
			addActionError(e.getMessage());
		}
		return SUCCESS;
	}

	/**
	 * 编辑
	 * 
	 * @return
	 * @throws Exception
	 */
	public String toEdit() throws Exception {
		if (StringUtils.isNotEmpty(goodsId)) {
			goods = goodsService.queryGoodsById(goodsId, getCurrentOperator());
			brandList = brandService.queryAllBrand("1", "1",
					getCurrentOperator());
			if (goods == null) {
				return ERROR;
			}
		}
		return INPUT;
	}

	/**
	 * 编辑
	 * 
	 * @return
	 * @throws Exception
	 */
	public String edit() throws Exception {
		try {
			String commonSalePrice = WebTools.getParameter("commonSalePrice");
			String goodsMoney = WebTools.getParameter("goodsMoney");
			try {
				goods.setCommonSalePrice((int) (Double
						.parseDouble(commonSalePrice) * 1000));
				if (StringUtils.isNotEmpty(goodsMoney)) {
					goods.setGoodsMoney((int) (Double.parseDouble(goodsMoney) * 1000));
				}
			} catch (Exception e) {
				flag = false;
				addActionError("格式转换异常!");
				return SUCCESS;
			}

			goodsService.update(goods, getCurrentOperator());
			if (goodsQuery == null) {
				goodsQuery = new GoodsQuery();
				goodsQuery.setPageSize(Constants.INIT_PAGESIZE);
				goodsQuery.setCurrentPage(Constants.INIT_CURRENTPAGE);
			}
			if (StringUtils.isNotEmpty(goId)) {
				goodsQuery.setGoodsId(goId);
			}
			if (StringUtils.isNotEmpty(brandId)) {
				goodsQuery.setGoodsCategoryId(brandId);
			}
			if (StringUtils.isNotEmpty(goodsName)) {
				goodsQuery.setGoodsName(goodsName);
			}
			if (StringUtils.isNotEmpty(rechargeMode)) {
				goodsQuery.setRechargeMode(rechargeMode);
			}
			return list();
		} catch (ManagerException e) {
			flag = false;
			addActionError(e.getMessage());
			redirectionUrl = "goods!list.htm";
			return SUCCESS;
		}
	}

	/**
	 * 修改商品状态
	 * 
	 * @return
	 * @throws Exception
	 */
	public String updateState() throws Exception {
		try {
			goodsService.updateState(id, goodsState, goodsId,
					getCurrentOperator());
		} catch (ManagerException e) {
			addActionError(e.getMessage());
			return ERROR;
		}

		return list();
	}

	/**
	 * 列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String list() throws Exception {
		if (goodsQuery == null) {
			goodsQuery = new GoodsQuery();
			goodsQuery.setPageSize(Constants.INIT_PAGESIZE);
			goodsQuery.setCurrentPage(Constants.INIT_CURRENTPAGE);
		}
		try {
			goodsQuery.setGoodsState("1");
			page = goodsService.queryGoods(goodsQuery, getCurrentOperator());
		} catch (ManagerException e) {
			addActionError(e.getMessage());
		}
		return LIST;
	}

	/**
	 * 搜索后的列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String goodsList() throws Exception {
		if (goodsQuery == null) {
			goodsQuery = new GoodsQuery();
			goodsQuery.setPageSize(Constants.INIT_PAGESIZE);
			goodsQuery.setCurrentPage(Constants.INIT_CURRENTPAGE);
		}
		try {
			String goodsId = goodsQuery.getGoodsId();
			String goodsCategoryId = goodsQuery.getGoodsCategoryId();
			String goodsName = goodsQuery.getGoodsName();
			String brandName = goodsQuery.getBrandName();
			goodsId = FilterStringSpaces.checkStringHasSpace(goodsId);
			goodsCategoryId = FilterStringSpaces
					.checkStringHasSpace(goodsCategoryId);
			goodsName = FilterStringSpaces.checkStringHasSpace(goodsName);
			brandName = FilterStringSpaces.checkStringHasSpace(brandName);
			goodsQuery.setGoodsId(goodsId);
			goodsQuery.setGoodsCategoryId(goodsCategoryId);
			goodsQuery.setGoodsName(goodsName);
			goodsQuery.setBrandName(brandName);
			page = goodsService.queryGoods(goodsQuery, getCurrentOperator());
		} catch (ManagerException e) {
			addActionError(e.getMessage());
		}
		return LIST;
	}

	/**
	 * 详情查询
	 * 
	 * @return
	 * @throws Exception
	 */
	public String detail() throws Exception {
		try {
			goods = goodsService.queryGoodsById(goodsId, getCurrentOperator());
			if (goods == null) {
				return ERROR;
			}
		} catch (ManagerException e) {
			addActionError(e.getMessage());
			return "game_error";
		}
		return "detail";
	}

	/**
	 * 删除
	 * 
	 * @return
	 * @throws Exception
	 */
	public String delete() throws Exception {
		try {
			goodsService.deleteByGoodsId(goodsId, getCurrentOperator());
		} catch (ManagerException e) {
			addActionError(e.getMessage());
		}
		return list();
	}

	public String inputSupply() throws Exception {
		/**
		 * 供应商状态, 0：不合作，1：合作
		 */
		supplyList = this.supplyManagerService.queryAllSupply("1",
				getCurrentOperator());
		goods = goodsService.queryGoodsById(goodsId, getCurrentOperator());
		return "input_supply";
	}

	public String saveGoodsSupply() throws Exception {
		try {
			goodsService.saveGoodsSupply(goods, getCurrentOperator());
			redirectionUrl = "goods!list.htm";
		} catch (ManagerException e) {
			flag = false;
			addActionError(e.getMessage());
		}
		return SUCCESS;
	}

	public String toSetPrice() throws Exception {
		try {
			templateNameList = gamePriceService.queryTemplateNameList(null,
					getCurrentOperator());
			goods = goodsService.queryGoodsById(goodsId, getCurrentOperator());
		} catch (ManagerException e) {
			addActionError(e.getMessage());
		}
		return "set_price";
	}

	public String setPrice() throws Exception {
		try {
			String salePrice = WebTools.getParameter("salePrice");
			goods.setInPrice(DataUtil.toInt(salePrice));
			String brandId = goods.getGoodsCategoryId();
			goods.setBrandId(brandId);
			this.goodsService.setPrice(goods, getCurrentOperator());
			redirectionUrl = "goods!list.htm";
		} catch (ManagerException e) {
			flag = false;
			addActionError(e.getMessage());
		}
		return SUCCESS;
	}

	public Goods getGoods() {
		return goods;
	}

	public void setGoods(Goods goods) {
		this.goods = goods;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getGoodsId() {
		return goodsId;
	}

	public void setGoodsId(String goodsId) {
		this.goodsId = goodsId;
	}

	public String getGoodsState() {
		return goodsState;
	}

	public void setGoodsState(String goodsState) {
		this.goodsState = goodsState;
	}

	public GoodsQuery getGoodsQuery() {
		return goodsQuery;
	}

	public void setGoodsQuery(GoodsQuery goodsQuery) {
		this.goodsQuery = goodsQuery;
	}

	public Pagination<Goods> getPage() {
		return page;
	}

	public void setPage(Pagination<Goods> page) {
		this.page = page;
	}

	public String getGoId() {
		return goId;
	}

	public void setGoId(String goId) {
		this.goId = goId;
	}

	public String getBrandId() {
		return brandId;
	}

	public void setBrandId(String brandId) {
		this.brandId = brandId;
	}

	public String getGoodsName() {
		return goodsName;
	}

	public void setGoodsName(String goodsName) {
		this.goodsName = goodsName;
	}

	public String getRechargeMode() {
		return rechargeMode;
	}

	public void setRechargeMode(String rechargeMode) {
		this.rechargeMode = rechargeMode;
	}

	public List<Supply> getSupplyList() {
		return supplyList;
	}

	public void setSupplyList(List<Supply> supplyList) {
		this.supplyList = supplyList;
	}

	public List<GoodsBrand> getBrandList() {
		return brandList;
	}

	public void setBrandList(List<GoodsBrand> brandList) {
		this.brandList = brandList;
	}

	public List<String> getTemplateNameList() {
		return templateNameList;
	}

	public void setTemplateNameList(List<String> templateNameList) {
		this.templateNameList = templateNameList;
	}

}
